package cn.stylefeng.guns.modular.rest.basic;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.stylefeng.guns.core.util.CangGuanRoleId;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicPutgoods;
import cn.stylefeng.guns.modular.basic.model.params.BasicAddressParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicDriverParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPrivatecarParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPutgoodsParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicDriverResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPrivatecarResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPutgoodsResult;
import cn.stylefeng.guns.modular.basic.service.BasicAddressService;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicDriverService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicPrivatecarService;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 *车、司机
 */
@Controller
@RequestMapping("/rest/driverAndCar")
public class restDriverAndCarController {

    @Autowired
    private BasicDriverService basicDriverService;

    @Autowired
    private BasicPrivatecarService basicPrivatecarService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private UserService userService;
    
    @Autowired
    private UserAop userAop;

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private BasicCarrierService basicCarrierService;
    
    /**
     * 上传本人照片
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew")
    @ResponseBody
    public ResponseData uploadsnew(@RequestParam("file") MultipartFile file,Integer id,BasicDriverParam basicDriverParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        basicDriverParam.setId(id);
        basicDriverParam.setSijiphotofileid(fileId);
        basicDriverService.update(basicDriverParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传身份证 正面照
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew2")
    @ResponseBody
    public ResponseData uploadsnew2(@RequestParam("file") MultipartFile file,Integer id,BasicDriverParam basicDriverParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        basicDriverParam.setId(id);
        basicDriverParam.setIdcarpohtofileid(fileId);
        basicDriverService.update(basicDriverParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传身份证 反面照
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew3")
    @ResponseBody
    public ResponseData uploadsnew3(@RequestParam("file") MultipartFile file,Integer id,BasicDriverParam basicDriverParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        basicDriverParam.setId(id);
        basicDriverParam.setMail(fileId);
        basicDriverService.update(basicDriverParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传驾驶证
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew4")
    @ResponseBody
    public ResponseData uploadsnew4(@RequestParam("file") MultipartFile file,Integer id,BasicDriverParam basicDriverParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        basicDriverParam.setId(id);
        basicDriverParam.setJiashizhengphotofileid(fileId);
        basicDriverService.update(basicDriverParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传从业证
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew5")
    @ResponseBody
    public ResponseData uploadsnew5(@RequestParam("file") MultipartFile file,Integer id,BasicDriverParam basicDriverParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        basicDriverParam.setId(id);
        basicDriverParam.setCongyezhengphotofileid(fileId);
        basicDriverService.update(basicDriverParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传车辆照片
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew6")
    @ResponseBody
    public ResponseData uploadsnew6(@RequestParam("file") MultipartFile file,Integer id,BasicPrivatecarParam BasicPrivatecarParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        BasicPrivatecarParam.setId(id);
        BasicPrivatecarParam.setCarphotofileid(fileId);
        basicPrivatecarService.update(BasicPrivatecarParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传行驶证
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew7")
    @ResponseBody
    public ResponseData uploadsnew7(@RequestParam("file") MultipartFile file,Integer id,BasicPrivatecarParam BasicPrivatecarParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        BasicPrivatecarParam.setId(id);
        BasicPrivatecarParam.setXingshizhengfileid(fileId);
        basicPrivatecarService.update(BasicPrivatecarParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传 营业许可证
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew8")
    @ResponseBody
    public ResponseData uploadsnew8(@RequestParam("file") MultipartFile file,Integer id,BasicPrivatecarParam BasicPrivatecarParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        BasicPrivatecarParam.setId(id);
        BasicPrivatecarParam.setXukezhengfileid(fileId);
        basicPrivatecarService.update(BasicPrivatecarParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 上传保险合同
     * @param file
     * @return
     */
    @RequestMapping("/uploadsnew9")
    @ResponseBody
    public ResponseData uploadsnew9(@RequestParam("file") MultipartFile file,Integer id,BasicPrivatecarParam BasicPrivatecarParam) {
        String fileId = this.fileInfoService.uploadFileCar(file);
        
        BasicPrivatecarParam.setId(id);
        BasicPrivatecarParam.setBaoxianhetongfileid(fileId);
        basicPrivatecarService.update(BasicPrivatecarParam);
        
        return ResponseData.success(fileId);
    }
    
    /**
     * 更新司机信息
     * @param file
     * @return
     */
    @RequestMapping("/updateDriver")
    @ResponseBody
    public ResponseData updateDriver(BasicDriverParam basicDriverParam) {
        basicDriverService.update(basicDriverParam);
        return ResponseData.success();
    }
    
    /**
     * 更新车辆信息
     * @param file
     * @return
     */
    @RequestMapping("/updatecar")
    @ResponseBody
    public ResponseData updatecar(BasicPrivatecarParam BasicPrivatecarParam) {
        basicPrivatecarService.update(BasicPrivatecarParam);
        return ResponseData.success();
    }

    /**
     * 查询我的信息
     */
    @RequestMapping("/findDriver")
    @ResponseBody
    public ResponseData findDriver(BasicDriverParam basicDriverParam) {
    	User user = userAop.getUser();
    	basicDriverParam.setUserId(user.getUserId());
    	BasicDriverResult basicDriverResult = basicDriverService.findBySpec(basicDriverParam,null);
        return ResponseData.success(basicDriverResult);
    }

    /**
     * 查询我的车辆信息
     */
    @RequestMapping("/findCar")
    @ResponseBody
    public ResponseData findCar(BasicDriverParam basicDriverParam) {
    	User user = userAop.getUser();
    	basicDriverParam.setUserId(user.getUserId());
    	BasicDriverResult basicDriverResult = basicDriverService.findBySpec(basicDriverParam,null);
    	BasicPrivatecarResult basicPrivatecarResult = new BasicPrivatecarResult();
    	if(basicDriverResult != null) {
    		BasicPrivatecarParam BasicPrivatecarParam = new BasicPrivatecarParam();
    		BasicPrivatecarParam.setDriverId1(Long.parseLong(basicDriverResult.getId().toString()));
    		basicPrivatecarResult = basicPrivatecarService.findBySpec(BasicPrivatecarParam, null,null);
    	}
        return ResponseData.success(basicPrivatecarResult);
    }

    /**
     * 查询承运商
     */
    @RequestMapping("/findCarrierList")
    @ResponseBody
    public ResponseData findCarrierList(BasicCarrierParam basicCarrierParam) {
    	User user = userAop.getUser();
    	basicCarrierParam.setDeliverId(user.getDeliverId());
    	List<BasicCarrierResult> list = basicCarrierService.findListBySpec(basicCarrierParam, null);
        return ResponseData.success(list);
    }

    /**
     * 校验是否完善信息
     */
    @RequestMapping("/checkmsg")
    @ResponseBody
    public ResponseData checkmsg(BasicDriverParam basicDriverParam) {
    	User user = userAop.getUser();
    	basicDriverParam.setUserId(user.getUserId());
    	BasicDriverResult basicDriverResult = basicDriverService.findBySpec(basicDriverParam,null);
    	if(basicDriverResult != null) {
    		if(ToolUtil.isEmpty(basicDriverResult.getIdcarpohtofileid()) || ToolUtil.isEmpty(basicDriverResult.getJiashizhengphotofileid()) ) {
    			return ResponseData.error("您得身份证或驾驶证信息不完善，请完善您得个人信息");
    		}
    		BasicPrivatecarParam BasicPrivatecarParam = new BasicPrivatecarParam();
    		BasicPrivatecarParam.setDriverId1(Long.parseLong(basicDriverResult.getId().toString()));
    		BasicPrivatecarResult basicPrivatecarResult = basicPrivatecarService.findBySpec(BasicPrivatecarParam, null,null);
    		if(basicPrivatecarResult != null) {
    			if(ToolUtil.isEmpty(basicPrivatecarResult.getXingshizhengfileid())) {
    				return ResponseData.error("您得行驶证信息不完善，请完善您得个人信息");
    			}
    		}
    	}
        return ResponseData.success();
    }

    /**
     * 司机注册
     */
    @RequestMapping("/register")
    @ResponseBody
    public ResponseData register(BasicDriverParam basicDriverParam,BasicPrivatecarParam basicPrivatecarParam
    		,String password,Long operateId) {
    	if(ToolUtil.isEmpty(basicDriverParam.getDriverName())) {
    		return ResponseData.error("请填写姓名");
    	}
    	if(ToolUtil.isEmpty(basicDriverParam.getTel())) {
    		return ResponseData.error("请填写手机号");
    	}
    	if(ToolUtil.isEmpty(password)) {
    		return ResponseData.error("请填写密码");
    	}
    	if(ToolUtil.isEmpty(basicPrivatecarParam.getPrivatecarCarnumber())) {
    		return ResponseData.error("请填写车牌号");
    	}
    	if(ToolUtil.isEmpty(basicPrivatecarParam.getPrivatecarSurevolume())) {
    		return ResponseData.error("请填写准载体积");
    	} else {
    		BigDecimal erbaiwu = new BigDecimal("250");
    		BigDecimal privatecarSurevolumebig = new BigDecimal(basicPrivatecarParam.getPrivatecarSurevolume()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		if(privatecarSurevolumebig.compareTo(erbaiwu) == 1) {
        		return ResponseData.error("准载体积不得超出250方，请更正");
    		} else {
    			basicPrivatecarParam.setPrivatecarSurevolume(privatecarSurevolumebig.toString());
    		}
    	}
		BigDecimal wushi = new BigDecimal("50");
		BigDecimal shi = new BigDecimal("10");
    	if(ToolUtil.isNotEmpty(basicPrivatecarParam.getPrivatecarCarlong())) {
    		BigDecimal privatecarCarlongbig = new BigDecimal(basicPrivatecarParam.getPrivatecarCarlong()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		if(privatecarCarlongbig.compareTo(wushi) == 1) {
        		return ResponseData.error("车长不得超出50米，请更正");
    		} 
    	} 
    	if(ToolUtil.isNotEmpty(basicPrivatecarParam.getPrivatecarCarwide())) {
    		BigDecimal privatecarCarwidebig = new BigDecimal(basicPrivatecarParam.getPrivatecarCarwide()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		if(privatecarCarwidebig.compareTo(shi) == 1) {
        		return ResponseData.error("车宽不得超出10米，请更正");
    		} 
    	} 
    	if(ToolUtil.isNotEmpty(basicPrivatecarParam.getPrivatecarCarhigh())) {
    		BigDecimal privatecarCarhighbig = new BigDecimal(basicPrivatecarParam.getPrivatecarCarhigh()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		if(privatecarCarhighbig.compareTo(shi) == 1) {
        		return ResponseData.error("车高不得超出10米，请更正");
    		} 
    	} 
    	User user = userService.getByAccount(basicDriverParam.getTel());
    	if(user != null) {
    		return ResponseData.error("该手机号已经注册");
    	}

        BasicOperate Operate = basicOperateService.getById(operateId);
        BasicPrivatecarParam basicPrivatecarParam1 = new BasicPrivatecarParam();
        basicPrivatecarParam1.setPrivatecarCarnumber(basicPrivatecarParam.getPrivatecarCarnumber());
        basicPrivatecarParam1.setDeliverId(Operate.getDeliverId());
        List<BasicPrivatecarResult> list = basicPrivatecarService.findBySpecList(basicPrivatecarParam1,null);
        if (list.size() >0){
    		return ResponseData.error("车牌号已存在");
        }
        
    	basicDriverParam.setDriverOperateId(Integer.parseInt(operateId.toString()));
        basicDriverParam.setDriverOperateName(Operate.getOperateName());
        basicDriverParam.setDriverNumber(basicDriverParam.getTel());
        basicDriverParam.setDeliverId(Operate.getDeliverId());
        basicDriverParam.setDeptId(Operate.getDeptId());
        basicDriverParam.setDrivetype("主驾");
        
        //添加账号
        UserDto userDto = new UserDto();
        userDto.setAccount(basicDriverParam.getTel());
        userDto.setPassword(password);
        userDto.setName(basicDriverParam.getDriverName());
        userDto.setSex(basicDriverParam.getSix().equals("男")?"M":"F");
        userDto.setPhone(basicDriverParam.getTel());
        userDto.setRoleId(CangGuanRoleId.SJRoleId.toString());
        userDto.setDeptId(Operate.getDeptId());
        userDto.setStatus("ENABLE");
        userDto.setDeliverId(Operate.getDeliverId());
        userDto.setOperateId(operateId);
        
        basicPrivatecarParam.setOperateId(operateId);
        basicPrivatecarParam.setOperateName(Operate.getOperateName());
        basicPrivatecarParam.setPrivatecarNumber(basicPrivatecarParam.getPrivatecarCarnumber());
        basicPrivatecarParam.setTooDeployvolume("0");
        basicPrivatecarParam.setDriver1(basicDriverParam.getDriverName());
        basicPrivatecarParam.setTel1(basicDriverParam.getTel());
        basicPrivatecarParam.setDeliverId(Operate.getDeliverId());
        basicPrivatecarParam.setDeptId(Operate.getDeptId());
        basicPrivatecarParam.setPrivatecarState("空闲");
        
        userService.addUser(userDto);
        user = userService.getByAccount(basicDriverParam.getTel());
        basicDriverParam.setUserId(user.getUserId());
        this.basicDriverService.addReturnId(basicDriverParam);
        basicPrivatecarParam.setDriverId1(Long.parseLong(basicDriverParam.getId().toString()));
        this.basicPrivatecarService.add(basicPrivatecarParam);
        return ResponseData.success();
    }

    /**
     * 收货单位注册
     */
    @RequestMapping("/registerUnit")
    @ResponseBody
    public ResponseData registerUnit(UserDto userDto) {
        if(userDto.getOperateId() == null) {
    		return ResponseData.error("未获取到项目id请联系管理员");
        }
    	if(ToolUtil.isEmpty(userDto.getGetUnit())) {
    		return ResponseData.error("请填写单位名称");
    	}
    	String putgoodsName = userDto.getGetUnit();
    	if (putgoodsName!=null&&putgoodsName!="") {
    		putgoodsName = putgoodsName.replace("& #40;", "(");
    		putgoodsName = putgoodsName.replace("& #41;", ")");
    		userDto.setGetUnit(putgoodsName);
    	}
    	if(ToolUtil.isEmpty(userDto.getName())) {
    		return ResponseData.error("请填写姓名");
    	}
    	if(ToolUtil.isEmpty(userDto.getAccount())) {
    		return ResponseData.error("请填写手机号");
    	}
    	if(ToolUtil.isEmpty(userDto.getPassword())) {
    		return ResponseData.error("请填写密码");
    	}
    	User user = userService.getByAccount(userDto.getAccount());
    	if(user != null) {
    		return ResponseData.error("该手机号已经注册");
    	}

        BasicOperate Operate = basicOperateService.getById(userDto.getOperateId());
        
        if(userDto.getGetUnitId() == null) {
        	BasicPutgoodsParam basicPutgoodsParam = new BasicPutgoodsParam();
        	basicPutgoodsParam.setPutgoodsName(userDto.getGetUnit());
        	basicPutgoodsParam.setDeptId(Operate.getDeptId());
        	BasicPutgoodsResult basicPutgoodsResult = basicPutgoodsService.findBySpec(basicPutgoodsParam, userDto.getOperateId());
        	if(basicPutgoodsResult != null) {
        		userDto.setGetUnitId(Long.parseLong(basicPutgoodsResult.getId()+""));
        	} else {
                BasicCustomerParam basicCustomerParam = new BasicCustomerParam();
                basicCustomerParam.setCustomerOperateId(Operate.getId());
                BasicCustomerResult basicCustomer = basicCustomerService.findBySpec(basicCustomerParam, userDto.getOperateId());
                if(basicCustomer == null) {
            		return ResponseData.error("未查询到客户信息请联系管理员");
                } 
	            BasicPutgoods bd = new BasicPutgoods();
	            BasicAddressParam bd2 = new BasicAddressParam();
	            bd.setUnitType("门店");
	            bd.setDeliverId(Operate.getDeliverId());
	            bd.setDeptId(Operate.getDeptId());
	            bd.setPutgoodsCustomerId(basicCustomer.getId());
	            bd.setPutgoodsOperateName(basicCustomer.getCustomerName());
				bd.setPutgoodsName(userDto.getGetUnit());
				basicPutgoodsService.save(bd);
        		userDto.setGetUnitId(Long.parseLong(bd.getId()+""));
                bd2.setPutgoodsId(Long.parseLong(bd.getId().toString()));
            	bd2.setLongitude("0");
                bd2.setLatitude("0");
                bd2.setSwitchWl("1");
                bd2.setSwitchBj("1500");
                bd2.setSwitchBj2("2000");
                basicAddressService.add(bd2);
        	}
        }
        
        //添加账号
        userDto.setSex(userDto.getSex().equals("男")?"M":"F");
        userDto.setPhone(userDto.getAccount());
        userDto.setRoleId(CangGuanRoleId.UNITRoleId.toString());
        userDto.setDeptId(Operate.getDeptId());
        userDto.setStatus("ENABLE");
        userDto.setDeliverId(Operate.getDeliverId());
        userService.addUser(userDto);
        return ResponseData.success();
    }

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;
	
	@Autowired
    private BasicAddressService basicAddressService;

    /**
     * 查询5条收货单位
     */
    @RequestMapping("/findbasicPutgoods5")
    @ResponseBody
    public ResponseData findbasicPutgoods5(BasicPutgoodsParam basicPutgoodsParam,Long operateId) {
        BasicOperate Operate = basicOperateService.getById(operateId);
        basicPutgoodsParam.setDeptId(Operate.getDeptId());
        List<BasicPutgoodsResult> res = basicPutgoodsService.findListBySpecLimit5(basicPutgoodsParam, operateId);
        return ResponseData.success(res);
    }
    
    

}
